package com.sdry.web.controller.zc.quality;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcEvaluateCriterionEntity;
import com.sdry.model.zc.ZcGasCriterionEntity;
import com.sdry.model.zc.ZcInspectionSheetEntity;
import com.sdry.model.zc.ZcLeakHuntingCriterionEntity;
import com.sdry.model.zc.ZcProductTestEntity;
import com.sdry.model.zc.ZcProductTestValueEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.service.zc.ZcCriterionService;
import com.sdry.service.zc.ZcInspectionSheetService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcInspectionSheetController   
 * @Description:  产品检验
 * @Author:       zc   
 * @CreateDate:   2019年1月4日 上午10:10:10   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("inspectionSheet")
public class ZcInspectionSheetController {

	@Resource
	private ZcInspectionSheetService zcInspectionSheetService;
	@Resource
	private ZcCriterionService zcCriterionService;
	/**
	 * 进入产品检验页面
	 * @return
	 */
	@RequestMapping("enterInspectionSheetPage")
	public String enterInspectionSheetPage(Model model){
		ZcLeakHuntingCriterionEntity zcLeakHuntingCriterionEntity = zcCriterionService.selectLeakHuntingCriterion();
		ZcEvaluateCriterionEntity zcEvaluateCriterionEntity = zcCriterionService.selectEvaluateCriterion();
		ZcGasCriterionEntity zcGasCriterionEntity = zcCriterionService.selectGasCriterion();
		model.addAttribute("zcLeakHuntingCriterionEntity", zcLeakHuntingCriterionEntity);
		model.addAttribute("zcEvaluateCriterionEntity", zcEvaluateCriterionEntity);
		model.addAttribute("zcGasCriterionEntity", zcGasCriterionEntity);
		return "/zc/quality/inspectionSheet";
	}
	/**
	 * 查询产品检验单列表
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectInspectionSheetList")
	public void selectInspectionSheetList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcInspectionSheetEntity> selectInspectionSheetList = zcInspectionSheetService.selectInspectionSheetList(zcQueryCriteria);
		int count = zcInspectionSheetService.countInspectionSheetList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectInspectionSheetList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过排产单查询该排产单下所有的检验结果
	 * @param request
	 * @param response
	 * @param remark02
	 */
	@RequestMapping("selectRemark03Byremark02")
	public void selectRemark03Byremark02(HttpServletRequest request,HttpServletResponse response,String remark02){
		List<ZcProductTestEntity> selectRemark03Byremark02 = zcInspectionSheetService.selectRemark03Byremark02(remark02);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectRemark03Byremark02, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过排产单号和检测结果类型查询产品检验数据
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectProductTestValue")
	public void selectProductTestValue(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		ZcProductTestValueEntity zcProductTestValueEntity = zcInspectionSheetService.selectProductTestValue(zcQueryCriteria);
		JSONObject json= JSONObject.fromObject(zcProductTestValueEntity);  
		try {
			if(zcProductTestValueEntity!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存产品检验单数据
	 * @param request
	 * @param response
	 * @param zcInspectionSheetEntity
	 */
	@RequestMapping("insertInspectionSheet")
	public void insertInspectionSheet(HttpServletRequest request,HttpServletResponse response,ZcInspectionSheetEntity zcInspectionSheetEntity,HttpSession httpSession){
		Date date = new Date();
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		long number = System.currentTimeMillis();
		zcInspectionSheetEntity.setIsDate(date);
		zcInspectionSheetEntity.setIsPerson(userName);
		zcInspectionSheetEntity.setIsNumber("IS"+number);
		int affact = zcInspectionSheetService.insertInspectionSheet(zcInspectionSheetEntity);
		int affactDetails = zcInspectionSheetService.insertInspectionSheetDetails(zcInspectionSheetEntity);
		if(affact>0&&affactDetails>0){
			try {
				ResponseUtil.write(response, 1);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			try {
				ResponseUtil.write(response, 0);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 通过单号查询产品检验详情表
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectInspectionSheetDetailsListByNumber")
	public void selectInspectionSheetDetailsListByNumber(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcInspectionSheetEntity> selectInspectionSheetDetailsListByNumber = zcInspectionSheetService.selectInspectionSheetDetailsListByNumber(zcQueryCriteria);
		int count = zcInspectionSheetService.countInspectionSheetDetailsListByNumber(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectInspectionSheetDetailsListByNumber, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @param idArr
	 */
	@RequestMapping("delInspectionSheetArr")
	public void delInspectionSheetArr(HttpServletRequest request, HttpServletResponse response,@RequestParam("isNumberArr[]") String[] isNumberArr){
		int affact = 0;
		for (String isNumber : isNumberArr) {
			affact += zcInspectionSheetService.delInspectionSheetArr(isNumber);
		}
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
